

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=&#34;auto&#34;>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/2.jpg">
  <link rel="icon" type="image/png" href="/img/2.jpg">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="">
  <meta name="author" content="K">
  <meta name="keywords" content="">
  <title>docker-compose-docker集群的快速编排 - K</title>

  <link  rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.4.1/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.staticfile.org/github-markdown-css/4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.staticfile.org/highlight.js/10.0.0/styles/github-gist.min.css" />
    
  

  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.2.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/">&nbsp;<strong>かい</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" data-toggle="modal" data-target="#modalSearch">&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;</a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" href="javascript:">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner intro-2" id="background" parallax=true
         style="background: url('/img/1.jpg') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="container page-header text-center fade-in-up">
            <span class="h2" id="subtitle">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2020-12-08 14:00" pubdate>
        2020年12月8日 下午
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      2.2k 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      33
       分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid">
  <div class="row">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-md">
      <div class="container nopadding-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto" id="post">
            <!-- SEO header -->
            <h1 style="display: none">docker-compose-docker集群的快速编排</h1>
            
            <div class="markdown-body" id="post-body">
              <!-- >这里充斥着迷梦，我正式逐梦之人 -->
<h2 id="docker-compose"><a href="#docker-compose" class="headerlink" title="docker-compose"></a>docker-compose</h2><ul>
<li><p>该项目是docker官方开源的一个项目</p>
</li>
<li><p>负责实现对docker集群的快速编排</p>
</li>
<li><p>docker-compose将所管理的容器分为三层</p>
<blockquote>
<p>project（工程） 也就是目录<br>service（服务）  yml文件中的容器(可以多个)<br>container（容器） </p>
</blockquote>
<ul>
<li>服务:一个应用的容器，实际上可以包含若干个相同镜像的容器实例</li>
<li>工程: 又一组关联的应用容器做成一个完整的业务单元，在docker-compose.yml中定义</li>
</ul>
</li>
<li><p>docker-compose目录下的所有文件组成一个工程，如果没有指定的工程名，那么就为当前目录名</p>
</li>
<li><p>一个工程可以包含多个服务，每个服务定义了容器运行的镜像，参数，依赖</p>
</li>
<li><p>一个服务当中可以包含多个容器实例</p>
</li>
<li><p>docker-compose没有实现负载均衡的问题，所以需要借助其他工具实现服务发现和负载均衡</p>
<blockquote>
<p>可以将docker-compose间接理解为ansible，可以使用ansible为多台主机部署服务，而docker-compose可以创建多个容器服务<br>docker-compose项目整个结构是由python完成的，调用docker服务来提供API对容器进行管理，因此操作的平台支持docker API，就可以通过docker-compose进行服务的编排</p>
</blockquote>
<ul>
<li>Docker Compose的工程配置文件默认为docker-compose.yml</li>
</ul>
</li>
</ul>
<h2 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h2><ul>
<li><p><a target="_blank" rel="noopener" href="https://docs.docker.com/compose/install/">https://docs.docker.com/compose/install/</a> 中可以找到安装方法</p>
</li>
<li><p>linux安装</p>
<pre><code class="hljs angelscript"># 将docker-compose脚本下载到/usr/local/bin/下
<span class="hljs-string">[root@localhost ~]</span># sudo curl -L <span class="hljs-string">&quot;https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)&quot;</span> -o /usr/local/bin/docker-compose
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
<span class="hljs-number">100</span>   <span class="hljs-number">651</span>  <span class="hljs-number">100</span>   <span class="hljs-number">651</span>    <span class="hljs-number">0</span>     <span class="hljs-number">0</span>    <span class="hljs-number">305</span>      <span class="hljs-number">0</span>  <span class="hljs-number">0</span>:<span class="hljs-number">00</span>:<span class="hljs-number">02</span>  <span class="hljs-number">0</span>:<span class="hljs-number">00</span>:<span class="hljs-number">02</span> --:--:--   <span class="hljs-number">305</span>
<span class="hljs-number">100</span> <span class="hljs-number">11.6</span>M  <span class="hljs-number">100</span> <span class="hljs-number">11.6</span>M    <span class="hljs-number">0</span>     <span class="hljs-number">0</span>  <span class="hljs-number">1650</span>k      <span class="hljs-number">0</span>  <span class="hljs-number">0</span>:<span class="hljs-number">00</span>:<span class="hljs-number">07</span>  <span class="hljs-number">0</span>:<span class="hljs-number">00</span>:<span class="hljs-number">07</span> --:--:-- <span class="hljs-number">2520</span>k
# 设置权限
<span class="hljs-string">[root@localhost ~]</span># chmod +x /usr/local/bin/docker-compose 
# 软连接到path路径
<span class="hljs-string">[root@localhost ~]</span># ln -s /usr/local/bin/docker-compose /usr/bin/
# 查看版本号
<span class="hljs-string">[root@localhost ~]</span># docker-compose -v
docker-compose version <span class="hljs-number">1.27</span><span class="hljs-number">.4</span>, build <span class="hljs-number">40524192</span></code></pre>
<h2 id="docker-compose-yml语法"><a href="#docker-compose-yml语法" class="headerlink" title="docker-compose.yml语法"></a>docker-compose.yml语法</h2></li>
<li><p>如果要编排的容器较多，建议使用目录将docker-compose.yml分隔开</p>
<blockquote>
<p>创建工程目录</p>
</blockquote>
<pre><code class="hljs autoit">[root<span class="hljs-symbol">@localhost</span> ~]<span class="hljs-meta"># mkdir test</span>
[root<span class="hljs-symbol">@localhost</span> ~]<span class="hljs-meta"># cd test/</span></code></pre>
<blockquote>
<p>编写yml文件</p>
</blockquote>
<pre><code class="hljs haml">[root@localhost test]# vim docker-compose.yml
nginx:
 container_name: nginx_lmk
 image: nginx:latest
 ports:
  -<span class="ruby"> <span class="hljs-string">&quot;80:80&quot;</span></span>
<span class="ruby"> <span class="hljs-symbol">volumes:</span></span>
<span class="ruby">  - <span class="hljs-regexp">/www/html</span><span class="hljs-regexp">/:/usr</span><span class="hljs-regexp">/share/nginx</span><span class="hljs-regexp">/html</span></span>
<span class="ruby"> <span class="hljs-symbol">hostname:</span> nginx_lmk.com</span>

<span class="ruby">nginx-<span class="hljs-symbol">two:</span></span>
<span class="ruby"> <span class="hljs-symbol">container_name:</span> nginx_kml</span>
<span class="ruby"> <span class="hljs-symbol">image:</span> <span class="hljs-symbol">nginx:</span>latest</span>
<span class="ruby"> <span class="hljs-symbol">ports:</span></span>
<span class="ruby">  - <span class="hljs-string">&quot;81:80&quot;</span></span>
<span class="ruby"> <span class="hljs-symbol">volumes:</span></span>
<span class="ruby">  - <span class="hljs-regexp">/www/html</span><span class="hljs-regexp">/:/usr</span><span class="hljs-regexp">/share/nginx</span><span class="hljs-regexp">/html</span></span>
<span class="ruby"> <span class="hljs-symbol">hostname:</span> nginx_kml.com</span>
</code></pre>
<blockquote>
<p>运行yml文件</p>
</blockquote>
<pre><code class="hljs angelscript"><span class="hljs-string">[root@localhost test]</span># docker-compose up -d 
Creating nginx_lmk ... done
Creating nginx_kml ... done
<span class="hljs-string">[root@localhost test]</span># docker-compose ps
  Name                 Command               State         Ports       
-----------------------------------------------------------------------
nginx_kml   /docker-entrypoint.sh ngin ...   Up      <span class="hljs-number">0.0</span><span class="hljs-number">.0</span><span class="hljs-number">.0</span>:<span class="hljs-number">81</span>-&gt;<span class="hljs-number">80</span>/tcp
nginx_lmk   /docker-entrypoint.sh ngin ...   Up      <span class="hljs-number">0.0</span><span class="hljs-number">.0</span><span class="hljs-number">.0</span>:<span class="hljs-number">80</span>-&gt;<span class="hljs-number">80</span>/tcp

</code></pre>
<blockquote>
<p>写入一个简单的index.html并访问</p>
</blockquote>
<pre><code class="hljs angelscript"><span class="hljs-string">[root@localhost test]</span># echo <span class="hljs-string">&#x27;hello_lmk&#x27;</span> &gt; /www/html/index.html
<span class="hljs-string">[root@localhost test]</span># curl <span class="hljs-number">192.168</span><span class="hljs-number">.100</span><span class="hljs-number">.211</span>
hello_lmk
<span class="hljs-string">[root@localhost test]</span># curl <span class="hljs-number">192.168</span><span class="hljs-number">.100</span><span class="hljs-number">.211</span>:<span class="hljs-number">81</span>
hello_lmk
</code></pre>

</li>
</ul>
<h2 id="docker-compose-命令"><a href="#docker-compose-命令" class="headerlink" title="docker-compose 命令"></a>docker-compose 命令</h2><ul>
<li>创建容器<blockquote>
<p>docker-compose up -d<br>-d 容器后台启动</p>
</blockquote>
</li>
<li>停止<blockquote>
<p>docker-compose stop<br>docker-compose kill</p>
</blockquote>
</li>
<li>删除容器<blockquote>
<p>docker-compose rm</p>
</blockquote>
</li>
<li>启动<blockquote>
<p>docker-compose start</p>
</blockquote>
</li>
<li>下载 （只下载yml文件的镜像）<blockquote>
<p>docker-compose pull</p>
</blockquote>
</li>
</ul>
<ul>
<li><p>开启的容器只有停止后才可以删除</p>
</li>
<li><p>接上面的yml文件</p>
<blockquote>
<p>如果将docker-compose.yml修改为nginx.yml 就需要指定文件名运行了</p>
</blockquote>
<pre><code class="hljs angelscript"><span class="hljs-string">[root@localhost test]</span># mv docker-compose.yml nginx.yml
<span class="hljs-string">[root@localhost test]</span># docker-compose -f nginx.yml -p myweb up -d
Creating nginx_lmk ... done
Creating nginx_kml ... done
 # -f 指定yml文件名
 # -p 指定项目名
<span class="hljs-string">[root@localhost test]</span># docker-compose -f nginx.yml -p myweb ps
  Name                 Command               State         Ports       
-----------------------------------------------------------------------
nginx_kml   /docker-entrypoint.sh ngin ...   Up      <span class="hljs-number">0.0</span><span class="hljs-number">.0</span><span class="hljs-number">.0</span>:<span class="hljs-number">81</span>-&gt;<span class="hljs-number">80</span>/tcp
nginx_lmk   /docker-entrypoint.sh ngin ...   Up      <span class="hljs-number">0.0</span><span class="hljs-number">.0</span><span class="hljs-number">.0</span>:<span class="hljs-number">80</span>-&gt;<span class="hljs-number">80</span>/tcp
# 这种方法很麻烦，所以尽量用不同的目录区别容器
</code></pre>
<h2 id="docker-compose部署wordpress"><a href="#docker-compose部署wordpress" class="headerlink" title="docker-compose部署wordpress"></a>docker-compose部署wordpress</h2></li>
<li><p>实例</p>
<blockquote>
<p>编写yml文件<br>以下为正规写法</p>
</blockquote>
<pre><code class="hljs dts">[root@localhost test]<span class="hljs-meta"># vim docker-compose.yml </span>

<span class="hljs-symbol">version:</span> <span class="hljs-string">&#x27;3&#x27;</span> <span class="hljs-meta"># 指定版本号</span>
<span class="hljs-symbol">services:</span>  <span class="hljs-meta"># 服务标签</span>
<span class="hljs-symbol"> db:</span>  <span class="hljs-meta"># 容器标签——随意写就是容器名</span>
<span class="hljs-symbol">  image:</span> mysql:<span class="hljs-number">5.7</span> <span class="hljs-meta"># 指定容器的镜像</span>
<span class="hljs-symbol">  volumes:</span> <span class="hljs-meta"># 将data_data映射到容器的mysql目录</span>
   - db_data:<span class="hljs-meta-keyword">/var/</span>lib/mysql
<span class="hljs-symbol">  restart:</span> always <span class="hljs-meta"># 容器遇到错误会重启</span>
<span class="hljs-symbol">  environment:</span> <span class="hljs-meta"># 定义参数 </span>
<span class="hljs-symbol">   MYSQL_ROOT_PASSWORD:</span> <span class="hljs-number">123.</span>com <span class="hljs-meta"># 设置mysql的root密码</span>
<span class="hljs-symbol">   MYSQL_DATABASE:</span> kml <span class="hljs-meta"># 创建mysql的一个库</span>
<span class="hljs-symbol">   MYSQL_USER:</span> lmk <span class="hljs-meta"># 创建一个mysql的用户</span>
<span class="hljs-symbol">   MYSQL_PASSWORD:</span> <span class="hljs-number">123.</span>com <span class="hljs-meta"># 设置mysql lmk这个用户的密码</span>
 
<span class="hljs-symbol"> wordpress:</span> <span class="hljs-meta"># 容器标签</span>
<span class="hljs-symbol">  depends_on:</span> <span class="hljs-meta"># 以依赖顺序启动 这里依赖的是上面的db容器，如果上面的失败这个也就不启动了 </span>
   - db
<span class="hljs-symbol">  image:</span> wordpress:latest <span class="hljs-meta"># 指定镜像</span>
<span class="hljs-symbol">  links:</span> <span class="hljs-meta"># 使用links可以将db容器的ip记录到该容器中</span>
   - db
<span class="hljs-symbol">  volumes:</span> <span class="hljs-meta"># 将wp_site映射到html目录</span>
   - wp_site:<span class="hljs-meta-keyword">/var/</span>www/html
<span class="hljs-symbol">  ports:</span> <span class="hljs-meta"># 指定映射端口 相当于 docker run -p这个</span>
   - <span class="hljs-string">&quot;80:80&quot;</span>
   - <span class="hljs-string">&quot;443:443&quot;</span>
<span class="hljs-symbol">  restart:</span> always <span class="hljs-meta"># 容器遇到错误重启  和 --restart 一样</span>
<span class="hljs-symbol">  environment:</span> <span class="hljs-meta"># 定义参数</span>
<span class="hljs-symbol">   WORDPRESS_DB_HOST:</span> db:<span class="hljs-number">3306</span> <span class="hljs-meta"># 指定要连接的数据库 这里db之前links了就不用写ip了</span>
<span class="hljs-symbol">   WORDPRESS_DB_USER:</span> lmk <span class="hljs-meta"># 指定数据库的用户</span>
<span class="hljs-symbol">   WORDPRESS_DB_PASSWORD:</span> <span class="hljs-number">123.</span>com <span class="hljs-meta"># 指定数据库lmk用户的密码</span>

<span class="hljs-symbol">volumes:</span> <span class="hljs-meta"># 创建卷</span>
<span class="hljs-symbol"> db_data:</span> <span class="hljs-meta"># 一个卷</span>
<span class="hljs-symbol"> wp_site:</span> <span class="hljs-meta"># 两个卷</span>
</code></pre>
<blockquote>
<p>启动容器</p>
</blockquote>
<pre><code class="hljs bash">[root@localhost <span class="hljs-built_in">test</span>]<span class="hljs-comment"># docker-compose up -d</span>
Creating test_db_1 ... <span class="hljs-keyword">done</span>
Creating test_wordpress_1 ... <span class="hljs-keyword">done</span></code></pre>
<blockquote>
<p>查看一些数据</p>
</blockquote>
<pre><code class="hljs autoit"><span class="hljs-meta"># yml文件最后创建的卷</span>
[root<span class="hljs-symbol">@localhost</span> test]<span class="hljs-meta"># docker volume ls</span>
DRIVER              VOLUME NAME
<span class="hljs-keyword">local</span>               test_db_data
<span class="hljs-keyword">local</span>               test_wp_site
<span class="hljs-meta"># 查看容器启动情况</span>
[root<span class="hljs-symbol">@localhost</span> test]<span class="hljs-meta"># docker-compose ps</span>
      Name                    Command              State              Ports            
---------------------------------------------------------------------------------------
test_db_1          docker-entrypoint.sh mysqld     Up      <span class="hljs-number">3306</span>/tcp, <span class="hljs-number">33060</span>/tcp         
test_wordpress_1   docker-entrypoint.sh apach      Up      <span class="hljs-number">0.0</span><span class="hljs-number">.0</span><span class="hljs-number">.0</span>:<span class="hljs-number">443</span>-&gt;<span class="hljs-number">443</span>/tcp,       
                   ...                                     <span class="hljs-number">0.0</span><span class="hljs-number">.0</span><span class="hljs-number">.0</span>:<span class="hljs-number">80</span>-&gt;<span class="hljs-number">80</span>/tcp  
<span class="hljs-meta"># 查看容器的挂载信息</span>
[root<span class="hljs-symbol">@localhost</span> test]<span class="hljs-meta"># docker inspect test_wordpress_1 </span>

        <span class="hljs-string">&quot;Mounts&quot;</span>: [
            &#123;
                <span class="hljs-string">&quot;Type&quot;</span>: <span class="hljs-string">&quot;volume&quot;</span>,<span class="hljs-meta"># 挂载类型</span>
                <span class="hljs-string">&quot;Name&quot;</span>: <span class="hljs-string">&quot;test_wp_site&quot;</span>, <span class="hljs-meta"># 卷名</span>
                <span class="hljs-string">&quot;Source&quot;</span>: <span class="hljs-string">&quot;/var/lib/docker/volumes/test_wp_site/_data&quot;</span>, <span class="hljs-meta"># 源地址(物理机地址)</span>
                <span class="hljs-string">&quot;Destination&quot;</span>: <span class="hljs-string">&quot;/var/www/html&quot;</span>, <span class="hljs-meta"># 挂载地址(容器地址)</span>
                <span class="hljs-string">&quot;Driver&quot;</span>: <span class="hljs-string">&quot;local&quot;</span>, <span class="hljs-meta"># 类型</span>
                <span class="hljs-string">&quot;Mode&quot;</span>: <span class="hljs-string">&quot;rw&quot;</span>, <span class="hljs-meta"># 可读写</span>
                <span class="hljs-string">&quot;RW&quot;</span>: <span class="hljs-literal">true</span>, <span class="hljs-meta">#是否读写</span>
                <span class="hljs-string">&quot;Propagation&quot;</span>: <span class="hljs-string">&quot;&quot;</span> <span class="hljs-meta"># 传播方向</span>
            &#125;
        ],
<span class="hljs-meta"># 进入目录 可以看到已经持久化出来了</span>
[root<span class="hljs-symbol">@localhost</span> test]<span class="hljs-meta"># cd /var/lib/docker/volumes/test_wp_site/_data</span>
[root<span class="hljs-symbol">@localhost</span> _data]<span class="hljs-meta"># ls</span>
index.php        wp-blog-header.php    wp-cron.php        wp-mail.php
license.txt      wp-comments-post.php  wp-includes        wp-settings.php
readme.html      wp-config.php         wp-links-opml.php  wp-signup.php
wp-activate.php  wp-config-sample.php  wp-load.php        wp-trackback.php
wp-admin         wp-content            wp-login.php       xmlrpc.php
</code></pre>

</li>
</ul>
<h2 id="指定网卡"><a href="#指定网卡" class="headerlink" title="指定网卡"></a>指定网卡</h2><ul>
<li>编写yml文件<pre><code class="hljs yaml">[<span class="hljs-string">root@localhost</span> <span class="hljs-string">test</span>]<span class="hljs-comment"># vim docker-compose.yml </span>
<span class="hljs-attr">version:</span> <span class="hljs-string">&#x27;3&#x27;</span> <span class="hljs-comment"># 版本</span>
<span class="hljs-attr">services:</span> <span class="hljs-comment">#服务</span>
 <span class="hljs-attr">web1:</span> <span class="hljs-comment">#容器名</span>
  <span class="hljs-attr">image:</span> <span class="hljs-string">nginx</span> <span class="hljs-comment"># 镜像</span>
  <span class="hljs-attr">ports:</span> <span class="hljs-comment"># 映射端口</span>
   <span class="hljs-bullet">-</span> <span class="hljs-string">&quot;8080:80&quot;</span>
  <span class="hljs-attr">container_name:</span> <span class="hljs-string">&#x27;web1&#x27;</span> <span class="hljs-comment"># 容器的名字</span>
  <span class="hljs-attr">networks:</span> <span class="hljs-comment"># 指定网卡</span>
   <span class="hljs-bullet">-</span> <span class="hljs-string">dev</span> <span class="hljs-comment"># dev网卡</span>
 <span class="hljs-attr">web2:</span> 
  <span class="hljs-attr">image:</span> <span class="hljs-string">nginx</span>
  <span class="hljs-attr">ports:</span>
   <span class="hljs-bullet">-</span> <span class="hljs-string">&quot;8082:80&quot;</span>
  <span class="hljs-attr">container_name:</span> <span class="hljs-string">&quot;web2&quot;</span>
  <span class="hljs-attr">networks:</span>
   <span class="hljs-bullet">-</span> <span class="hljs-string">pro</span>
   <span class="hljs-bullet">-</span> <span class="hljs-string">dev</span>
 <span class="hljs-attr">web3:</span>
  <span class="hljs-attr">image:</span> <span class="hljs-string">nginx</span>
  <span class="hljs-attr">ports:</span>
   <span class="hljs-bullet">-</span> <span class="hljs-string">&#x27;8083:80&#x27;</span>
  <span class="hljs-attr">container_name:</span> <span class="hljs-string">&#x27;web3&#x27;</span>
  <span class="hljs-attr">networks:</span>
   <span class="hljs-bullet">-</span> <span class="hljs-string">pro</span>

<span class="hljs-attr">networks:</span> <span class="hljs-comment"># 创建网卡</span>
 <span class="hljs-attr">dev:</span> <span class="hljs-comment"># 创建dev网卡</span>
  <span class="hljs-attr">driver:</span> <span class="hljs-string">bridge</span> <span class="hljs-comment"># 指定网卡类型</span>
 <span class="hljs-attr">pro:</span>
  <span class="hljs-attr">driver:</span> <span class="hljs-string">bridge</span></code></pre>
<blockquote>
<p>运行yml文件</p>
</blockquote>
<pre><code class="hljs gams">[root@localhost test]# docker-compose up -d 
<span class="hljs-function"><span class="hljs-title">Creating</span></span> web2 ... done
<span class="hljs-function"><span class="hljs-title">Creating</span></span> web1 ... done
<span class="hljs-function"><span class="hljs-title">Creating</span></span> web3 ... done</code></pre>
<blockquote>
<p>查看网络</p>
</blockquote>
<pre><code class="hljs routeros"><span class="hljs-comment"># 多出来两块网卡了</span>
[root@localhost test]# docker<span class="hljs-built_in"> network </span>ls
NETWORK ID          NAME                DRIVER              SCOPE
8e8ce96b6c6b       <span class="hljs-built_in"> bridge </span>            <span class="hljs-built_in"> bridge </span>             local
dc088ea51e19        host                host                local
cec68349a7d5        none                <span class="hljs-literal">null</span>                local
e6b06fcb2ec9        test_default       <span class="hljs-built_in"> bridge </span>             local
2ed0fa1a71ea        test_dev           <span class="hljs-built_in"> bridge </span>             local
0bc547f8aa6e        test_pro           <span class="hljs-built_in"> bridge </span>             local

<span class="hljs-comment"># 查看映射端口</span>
[root@localhost test]# docker-compose<span class="hljs-built_in"> port </span><span class="hljs-attribute">--protocol</span>=tcp web1 80
0.0.0.0:8080
[root@localhost test]# docker-compose<span class="hljs-built_in"> port </span><span class="hljs-attribute">--protocol</span>=tcp web2 80
0.0.0.0:8082
[root@localhost test]# docker-compose<span class="hljs-built_in"> port </span><span class="hljs-attribute">--protocol</span>=tcp web3 80
0.0.0.0:8083

<span class="hljs-comment"># 访问本机的映射端口即可</span>
<span class="hljs-comment"># 另外 web1 和 web3 不可以通讯，因为不是一块网卡</span>
<span class="hljs-comment"># web2可以和web1和web3通讯</span></code></pre>

</li>
</ul>
<h2 id="build的使用"><a href="#build的使用" class="headerlink" title="build的使用"></a>build的使用</h2><ul>
<li>build可以指定dockerfile文件</li>
<li>先将dockerfile文件构建成镜像，在通过镜像启动容器<blockquote>
<p>创建目录结构_写入dockerfile文件</p>
</blockquote>
<pre><code class="hljs autoit">[root<span class="hljs-symbol">@localhost</span> test]<span class="hljs-meta"># mkdir dockerfile</span>
[root<span class="hljs-symbol">@localhost</span> test]<span class="hljs-meta"># cd dockerfile/</span>
[root<span class="hljs-symbol">@localhost</span> dockerfile]<span class="hljs-meta"># mkdir htdocs</span>
[root<span class="hljs-symbol">@localhost</span> dockerfile]<span class="hljs-meta"># echo <span class="hljs-string">&#x27;hello&#x27;</span> &gt; htdocs/index.html</span>
[root<span class="hljs-symbol">@localhost</span> dockerfile]<span class="hljs-meta"># vim Dockerfile</span>
FROM httpd
ADD htdocs /usr/<span class="hljs-keyword">local</span>/apache2/htdocs
VOLUME /usr/<span class="hljs-keyword">local</span>/apache2/htdocs

[root<span class="hljs-symbol">@localhost</span> test]<span class="hljs-meta"># vim docker-compose.yml</span>
version: <span class="hljs-string">&#x27;3&#x27;</span> <span class="hljs-meta"># 版本号</span>
services: <span class="hljs-meta"># 服务</span>
 web_httpd: <span class="hljs-meta"># 容器名</span>
  build: ./dockerfile <span class="hljs-meta"># dockerfile文件地址</span>
  ports: <span class="hljs-meta"># 映射端口号</span>
   - <span class="hljs-string">&#x27;6061:80&#x27;</span> 
  container_name: web123 <span class="hljs-meta"># 容器内的名字</span>
  networks: <span class="hljs-meta"># 指定网卡</span>
   - my_net 
networks: <span class="hljs-meta"># 创建网卡</span>
 my_net:
  driver: bridge
</code></pre>

</li>
</ul>
<blockquote>
<p>启动docker-compose</p>
</blockquote>
<pre><code class="hljs routeros">[root@localhost dockerfile]# docker-compose up -d
WARNING: Found orphan containers (web3, test_db_1, web2, web1, test_wordpress_1) <span class="hljs-keyword">for</span> this project. <span class="hljs-keyword">If</span> you removed <span class="hljs-keyword">or</span> renamed this<span class="hljs-built_in"> service </span><span class="hljs-keyword">in</span> your compose file, you can <span class="hljs-builtin-name">run</span> this command with the --remove-orphans flag <span class="hljs-keyword">to</span> clean it up.
Building web_httpd
<span class="hljs-keyword">Step</span> 1/3 : <span class="hljs-keyword">FROM</span> httpd:latest
 ---&gt; 417af7dc28bc
<span class="hljs-keyword">Step</span> 2/3 : <span class="hljs-builtin-name">ADD</span> htdocs /usr/local/apache2/htdocs
 ---&gt; 3038a3891823
<span class="hljs-keyword">Step</span> 3/3 : VOLUME /usr/local/apache2/htdocs
 ---&gt; Running <span class="hljs-keyword">in</span> 2158337298da
Removing intermediate container 2158337298da
 ---&gt; df9020772424

Successfully built df9020772424
Successfully tagged test_web_httpd:latest
WARNING: Image <span class="hljs-keyword">for</span><span class="hljs-built_in"> service </span>web_httpd was built because it did <span class="hljs-keyword">not</span> already exist. <span class="hljs-keyword">To</span> rebuild this image you must use `docker-compose build` <span class="hljs-keyword">or</span> `docker-compose up --build`.
Creating web123 <span class="hljs-built_in">..</span>. done
<span class="hljs-comment"># 访问</span>
[root@localhost dockerfile]# curl 192.168.100.211:6061
hello</code></pre>
<ul>
<li>会先构建镜像，在基于镜像创建容器</li>
</ul>
<h2 id="容器扩展"><a href="#容器扩展" class="headerlink" title="容器扩展"></a>容器扩展</h2><ul>
<li>根据上面的docker-compose.yml文件扩展5台容器<pre><code class="hljs yaml">[<span class="hljs-string">root@localhost</span> <span class="hljs-string">test</span>]<span class="hljs-comment"># vim docker-compose.yml</span>
<span class="hljs-attr">version:</span> <span class="hljs-string">&#x27;3&#x27;</span>
<span class="hljs-attr">services:</span>
 <span class="hljs-attr">web_httpd:</span>
  <span class="hljs-attr">build:</span> <span class="hljs-string">./dockerfile</span>
  <span class="hljs-attr">ports:</span>
   <span class="hljs-bullet">-</span> <span class="hljs-string">&#x27;80&#x27;</span>
  <span class="hljs-attr">networks:</span>
   <span class="hljs-bullet">-</span> <span class="hljs-string">my_net</span>
<span class="hljs-attr">networks:</span>
 <span class="hljs-attr">my_net:</span>
  <span class="hljs-attr">driver:</span> <span class="hljs-string">bridge</span></code></pre>
<blockquote>
<p>扩展容器</p>
</blockquote>
<pre><code class="hljs gams">[root@localhost test]# docker-compose up -d --scale web_httpd=<span class="hljs-number">5</span> --<span class="hljs-keyword">no</span>-recreate
<span class="hljs-function"><span class="hljs-title">Creating</span></span> test_web_httpd_1 ... done
<span class="hljs-function"><span class="hljs-title">Creating</span></span> test_web_httpd_2 ... done
<span class="hljs-function"><span class="hljs-title">Creating</span></span> test_web_httpd_3 ... done
<span class="hljs-function"><span class="hljs-title">Creating</span></span> test_web_httpd_4 ... done
<span class="hljs-function"><span class="hljs-title">Creating</span></span> test_web_httpd_5 ... done
# --scale 容器的名字=几台
# --<span class="hljs-keyword">no</span>-recreate  不回重复扩展容器</code></pre>


</li>
</ul>
<h2 id="docker-compose-yml补充"><a href="#docker-compose-yml补充" class="headerlink" title="docker-compose.yml补充"></a>docker-compose.yml补充</h2><ul>
<li>volumes_from 挂载容器卷 <pre><code class="hljs dts"><span class="hljs-symbol"> volumes_from:</span>
   - data1
   - data2
<span class="hljs-meta"># 将本地的容器卷挂载到当前这个容器</span></code></pre></li>
<li>dns 指定dns地址<pre><code class="hljs ldif"><span class="hljs-attribute">dns:8.8.8.8 # 指定一个dns</span>
<span class="hljs-attribute"></span>
<span class="hljs-attribute">dns</span>: # 指定多个dns
 - 114.114.114.114
 - 8.8.8.8</code></pre></li>
</ul>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/docker/">docker</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/docker/">docker</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">本博客所有文章是以学习为目的，如果有不对的地方可以一起交流沟通共同学习 邮箱:1248287831@qq.com！</p>
              
              
                <div class="post-prevnext row">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2020/12/09/docker-machine/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">docker-machine</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2020/12/07/docker-%E5%AE%B9%E5%99%A8%E6%8C%81%E4%B9%85%E5%8C%96/">
                        <span class="hidden-mobile">docker-容器持久化</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div id="tocbot"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    
  </main>

  
    <a id="scroll-top-button" href="#" role="button">
      <i class="iconfont icon-arrowup" aria-hidden="true"></i>
    </a>
  

  
    <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
  

  

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://space.bilibili.com/39165374" target="_blank" rel="nofollow noopener"><span>LMK</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
  </div>
  
  <div class="statistics">
    
    

    
      
        <!-- 不蒜子统计PV -->
        <span id="busuanzi_container_site_pv" style="display: none">
            总访问量 
            <span id="busuanzi_value_site_pv"></span>
             次
          </span>
      
      
        <!-- 不蒜子统计UV -->
        <span id="busuanzi_container_site_uv" style="display: none">
            总访客数 
            <span id="busuanzi_value_site_uv"></span>
             人
          </span>
      
    
  </div>


  

  
</footer>

<!-- SCRIPTS -->
<script  src="https://cdn.staticfile.org/jquery/3.4.1/jquery.min.js" ></script>
<script  src="https://cdn.staticfile.org/twitter-bootstrap/4.4.1/js/bootstrap.min.js" ></script>
<script  src="/js/debouncer.js" ></script>
<script  src="/js/main.js" ></script>

<!-- Plugins -->


  
    <script  src="/js/lazyload.js" ></script>
  



  



  <script defer src="https://cdn.staticfile.org/clipboard.js/2.0.6/clipboard.min.js" ></script>
  <script  src="/js/clipboard-use.js" ></script>



  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>





  <script  src="https://cdn.staticfile.org/tocbot/4.11.1/tocbot.min.js" ></script>
  <script>
    $(document).ready(function () {
      var boardCtn = $('#board-ctn');
      var boardTop = boardCtn.offset().top;

      tocbot.init({
        tocSelector: '#tocbot',
        contentSelector: '#post-body',
        headingSelector: 'h1,h2,h3,h4,h5,h6',
        linkClass: 'tocbot-link',
        activeLinkClass: 'tocbot-active-link',
        listClass: 'tocbot-list',
        isCollapsedClass: 'tocbot-is-collapsed',
        collapsibleClass: 'tocbot-is-collapsible',
        collapseDepth: 0,
        scrollSmooth: true,
        headingsOffset: -boardTop
      });
      if ($('.toc-list-item').length > 0) {
        $('#toc').css('visibility', 'visible');
      }
    });
  </script>



  <script  src="https://cdn.staticfile.org/typed.js/2.0.11/typed.min.js" ></script>
  <script>
    var typed = new Typed('#subtitle', {
      strings: [
        '  ',
        "docker-compose-docker集群的快速编排&nbsp;",
      ],
      cursorChar: "_",
      typeSpeed: 70,
      loop: false,
    });
    typed.stop();
    $(document).ready(function () {
      $(".typed-cursor").addClass("h2");
      typed.start();
    });
  </script>



  <script  src="https://cdn.staticfile.org/anchor-js/4.2.2/anchor.min.js" ></script>
  <script>
    anchors.options = {
      placement: "right",
      visible: "hover",
      
    };
    var el = "h1,h2,h3,h4,h5,h6".split(",");
    var res = [];
    for (item of el) {
      res.push(".markdown-body > " + item)
    }
    anchors.add(res.join(", "))
  </script>



  <script  src="/js/local-search.js" ></script>
  <script>
    var path = "/local-search.xml";
    var inputArea = document.querySelector("#local-search-input");
    inputArea.onclick = function () {
      searchFunc(path, 'local-search-input', 'local-search-result');
      this.onclick = null
    }
  </script>



  <script  src="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.css" />

  <script>
    $('#post img:not(.no-zoom img, img[no-zoom]), img[zoom]').each(
      function () {
        var element = document.createElement('a');
        $(element).attr('data-fancybox', 'images');
        $(element).attr('href', $(this).attr('src'));
        $(this).wrap(element);
      }
    );
  </script>















</body>
</html>
